package net.anjingnorak.javabase.connection;

import java.sql.ResultSet;

public class SQLExecution {
	/**
	 * Meng-execute sql script dan mengembalikannya berupa Result Set 
	 * 
	 * @param query
	 * @return Result Set
	 * @throws Exception
	 */
	public static ResultSet executeQuery(String query) throws Exception{
		return Connection.dbConnection.createStatement().executeQuery(query);
	}
	
	/**
	 * Meng-execute sql script dan mengembalikannya berupa boolean, true = berhasil dan false = gagal
	 * 
	 * @param query
	 * @return boolean
	 * @throws Exception
	 */
	public static boolean executeUpdate(String query) throws Exception{ 
		int update = Connection.dbConnection.createStatement().executeUpdate(query);
		switch(update){
			case 0 : return false;
			default: return true;
		}
	}
	
	/**
	 * mengubah sql script menjadi Command
	 * 
	 * @param query
	 * @return Command
	 */
	public static Command makeCommandQuery(String query){
		Command command = new Command();
		command.setQuery(query);
		return command;
	}
	
	/**
	 * Mengubah storeProcedure(nama store procedue) menjadi Command
	 * 
	 * @param storeProcedure
	 * @return Command
	 */
	public static Command makeCommandSP(String storeProcedure){
		Command command = new Command();
		command.setStoreProcedure(storeProcedure);
		return command;
	}
	
	/**
	 * Mengubah store procedure dan parameternya menjadi Command
	 * 
	 * @param storeProcedure
	 * @param parameterValues
	 * @return Command
	 */
	public static Command makeCommandSP(String storeProcedure, Object[] parameterValues){
		Command command = new Command();
		command.setStoreProcedure(storeProcedure);
		command.setParameterValue(parameterValues);
		return command;
	}
	
	/**
	 * Mengubah store procedure, nama parameter, isi parameter menjadi Command
	 * 
	 * @param storeProcedure
	 * @param parameterNames
	 * @param parameterValues
	 * @return Command
	 */
	public static Command makeCommandSP(String storeProcedure, String[] parameterNames, Object[] parameterValues){
		Command command = new Command();
		command.setStoreProcedure(storeProcedure);
		command.setParameterName(parameterNames);
		command.setParameterValue(parameterValues);
		return command;
	}
}
